/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 *    6, April, 2011. Created
 * 10, Feb, 2014 Trivial changes in order to make the code compliant with Java 1.7
 */

package portfolio.dbHost.dataBases.dbTable;

import java.sql.Connection;
//import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.DBase;
import portfolio.dbHost.dataBases.dbTable.tblColumn.ColumnFactory;
import portfolio.dbHost.dataBases.dbTable.tblColumn.MysqlColumn;

/**
 *
 * @author aca ldero
 */
public class MysqlTable extends Table
{
    public MysqlTable (DBase dBaseParam, String sTableParam) throws Exception
    {
        super (dBaseParam, sTableParam);
    }
    public MysqlTable (DBase dBaseParam) throws Exception
    {
        super (dBaseParam);
    }

    /***********************************************************************
    * Return: An array of vectors, each vector contains an attribute of    *
    * a table that fits the DESCRIBE <table> sql command; the attribute on *
    * vector 0 corresponds to the extreme left column of the Describe      *
    * command and so on until column 6 or vector 5. The describe columns   *
    * are the following:                                                   *
    * Field   - 0                                                          *
    * Type    - 1                                                          *
    * Null    - 2                                                          *
    * Key     - 3                                                          *
    * Default - 4                                                          *
    * Extra   - 5                                                          *
    ************************************************************************/
    @Override
    public Vector[] describeAll(Connection connectParam, String sDBNameParam, String sTable) throws Exception
    {
        if (connectParam == null)
            throw new Exception ("null connection.");
        if (sDBNameParam == null)
            throw new Exception ("null database name.");
        if (sTable == null)
            throw new Exception ("null table name.");

        Vector [] outVector = null;
        try {
            Statement statement = connectParam.createStatement ();
            statement.execute ("USE " + sDBNameParam + ";");
            ResultSet resultSet = statement.executeQuery ("DESCRIBE " + sTable + ";");
//            String sNativeStmt = connectParam.nativeSQL("SELECT column_name FROM " + "INFORMATION_SCHEMA.COLUMNS;");
//            ResultSet resultSet = statement.executeQuery (sNativeStmt);
            if (!resultSet.next ()) {
                throw new Exception ("describeTable method: resultSet = null.");
            }
            ResultSetMetaData rsmd = resultSet.getMetaData ();
            int iColCountTmp = rsmd.getColumnCount();

            if (iColCountTmp == 0) {
                throw new Exception ("null iColumnCount.");
            }
            outVector = new Vector [iColCountTmp];
            for (int iIdx = 0; iIdx < iColCountTmp; iIdx++) {
                outVector [iIdx] = new Vector ();
            }

            do {
                String sField = resultSet.getString (1);
                outVector [1 -1].addElement (sField);
                String sType = resultSet.getString (2);
                outVector [2 -1].addElement (sType);
                String sNull = resultSet.getString (3);
                sNull        = (sNull.equals ("YES"))? " NULL": " NOT NULL";
                outVector [3 -1].addElement (sNull);
                String sKey = resultSet.getString (4);
                outVector [4 -1].addElement (sKey);
                String sDefault = resultSet.getString (5);
                outVector [5 -1].addElement (sDefault);
                String sExtra = resultSet.getString (6);
                outVector [6 -1].addElement (sExtra);
            } while (resultSet.next ());
        }
        catch (SQLException sqlex) {
            throw new Exception (sqlex);
        }

        return (outVector);
    }

    @Override
    public void buildColumns() throws Exception, SQLException
    {
//        Vector [] vTemp = Table.describeAll (connection, sDBName, sTableName);
//        Vector [] vTemp = this.describeAll ();
        Vector [] vTemp = this.describeAll(connection, sDBName, sTableName);
        vColumns = vTemp [0];
        iColumnCount = vTemp [0].size();
        if (iColumnCount != 0) {
            columns = new MysqlColumn [iColumnCount];
            //System.out.println ("sTable " + iColumnCount);
            for (int iIdx = 0; iIdx < iColumnCount; iIdx++) {
                columns [iIdx] = new ColumnFactory ().getColumnDBType(Host.DB_TYPE_MYSQL, this);//Column (this);
                columns [iIdx].setField (vTemp[0].get(iIdx).toString());
                ( columns [iIdx]).setType  (vTemp[1].get(iIdx).toString());
                ( columns [iIdx]).setNull  (vTemp[2].get(iIdx).toString());
                ( columns [iIdx]).setKey   (vTemp[3].get(iIdx).toString());
            }
        }
    }
}
